package com.lvmama.rhino.collector.action;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 吴川通
 * @version V1.0
 * @Title: BadRequestControllerAdvice
 * @Package com.lvmama.rhino.collector.action
 * @Description: 用于处理spring报400错误而后台未输出错误日志的情况
 * @date 2016/10/26 13:38
 */
@ControllerAdvice
public class BadRequestControllerAdvice {
    protected static Logger logger = Logger.getLogger(BadRequestControllerAdvice.class);

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String processBadRequestException(NativeWebRequest request, Exception e) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request.getNativeRequest();
        logger.error(httpServletRequest.getRequestURI() + " error,cause:" + e.getMessage());
        request.setAttribute("ex", e, 0);
        return "/400.jsp";
    }
}
